package spacecraft;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Random;

import markgame2d.engine.DefaultCollideable;
import markgame2d.engine.MarkGameListUtils;
import markgame2d.engine.MarkImageLoader;
import markgame2d.engine.MarkMath;
import markgame2d.engine.MarkScreen;
import markgame2d.engine.MarkSimpleClip;


public class GameScreen extends MarkScreen {

	public static final String CRAFT_FILE = "spacecraft/images/craft.png";
	public static final String MISSILE_FILE = "spacecraft/images/missile.png";
	public static final String ALIEN_FILE = "spacecraft/images/alien.png";

	//public static final String FIRE_FILE = "spacecraft/sounds/shot.wav";
	//public static final String BOOM_FILE = "spacecraft/sounds/boom.wav";
	
	public static final int MISSILE_VEL = 500;
	public static final long SHOOT_DELAY = 80;
	public static final int CRAFT_VEL = 250;
	public static final int ALIENS_DELAY = 1000 / 10;// 10 aliens/sec
	public static final float ALIEN_VEL = 80;

	public static final int POINTS_BY_KILL_ALIEN = 10;
	public static final int POINTS_BY_ALIEN_PASS = -50;

	public static final int LEFT_KEY = KeyEvent.VK_LEFT;
	public static final int RIGHT_KEY = KeyEvent.VK_RIGHT;
	public static final int UP_KEY = KeyEvent.VK_UP;
	public static final int DOWN_KEY = KeyEvent.VK_DOWN;
	public static final int SHOOT_KEY = KeyEvent.VK_CONTROL;
	public static final int PAUSE_KEY = KeyEvent.VK_P;
	
	SpaceCraft game;

	Random random = new Random();

	MarkImageLoader imageLoader;

	//public MarkSimpleClip boomSound;
	//public MarkSimpleClip fireSound;
	
	Craft craft;
	LinkedList<CraftShield> shields;
	LinkedList<Missile> missiles;
	LinkedList<Alien> aliens;
	
	DefaultCollideable aliensLimit;
	DefaultCollideable missilesLimit;
	
	long lastShoot;
	long lastAlien;

	int points;
	int lives;

	public GameScreen() {
		this.game = SpaceCraft.getInstance();
		imageLoader = new MarkImageLoader();
		
		/*try{
			boomSound = new MarkSimpleClip(BOOM_FILE);
			fireSound = new MarkSimpleClip(FIRE_FILE);
		}catch(Exception e){
			e.printStackTrace();
		}*/
		
		aliensLimit = new DefaultCollideable(-30, 0, 5, SpaceCraft.HEIGHT);
		missilesLimit = new DefaultCollideable(SpaceCraft.WIDTH+30, 0, 5, SpaceCraft.HEIGHT);
		
		reset();
	}
	
	public void reset(){
		craft = new Craft(game.getWidth() / 2, game.getHeight() / 2);
		shields = new LinkedList<CraftShield>();
		for (int angleDiv : new int[]{0,1,2,3,4,5,6,7,8,9})
			shields.add(new CraftShield(craft, MarkMath.TWO_PI * angleDiv / 10));
		missiles = new LinkedList<Missile>();
		aliens = new LinkedList<Alien>();
		lastShoot = 0;
		lastAlien = 0;
		points = 0;
		lives = 5;
	}

	public void paint(Graphics2D g) {
		
		craft.draw(g);
		MarkGameListUtils.draw(shields, g);

		MarkGameListUtils.draw(missiles, g);
		MarkGameListUtils.draw(aliens, g);

		g.setColor(Color.WHITE);
		g.drawString("Points: " + points, 10, 15);
		g.drawString("Lives: " + lives, 10, 35);
	}

	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == PAUSE_KEY){
			game.setScreen(game.pauseScreen);
		}
	}
	
	public void update(float time) {

		craft.update(time);
		MarkGameListUtils.update(shields, time);

		MarkGameListUtils.update(missiles, time);
		MarkGameListUtils.removeDead(missiles);
		
		tryShoot();

		MarkGameListUtils.update(aliens, time);
		MarkGameListUtils.removeDead(aliens);

		tryCreateAlien();

		MarkGameListUtils.testCollision(aliensLimit, aliens);
		MarkGameListUtils.testCollision(missilesLimit, missiles);
		MarkGameListUtils.testCollision(craft, aliens);
		MarkGameListUtils.testCollision(missiles, aliens);
		MarkGameListUtils.testCollision(shields, aliens);
		
		if(lives < 0){
			game.setScreen(game.gameOverScreen);
		}
		
	}

	void shoot(){

		/*Missile missile = new Missile(craft.x + craft.width, craft.y + craft.height/2);
		missile.y -= missile.height/2;
		missiles.add(missile);*/
		
		Missile missile1 = new Missile(craft.x + craft.width, craft.y + craft.height/2);
		missile1.y -= missile1.height/2;
		missile1.y -= missile1.height;
		
		Missile missile2 = new Missile(craft.x + craft.width, craft.y + craft.height/2);
		missile2.y -= missile2.height/2;
		missile2.y += missile2.height;
		
		missiles.add(missile1);
		missiles.add(missile2);
		
		//fireSound.play();
	}
	
	void tryShoot() {
		if (game.getKeys()[SHOOT_KEY]) {
			if (System.currentTimeMillis() - lastShoot > SHOOT_DELAY) {
				shoot();
				lastShoot = System.currentTimeMillis();
			}
		}
	}

	void tryCreateAlien() {
		long diff = System.currentTimeMillis() - lastAlien;
		if (diff >= ALIENS_DELAY) {
			Alien alien = new Alien(SpaceCraft.WIDTH, 0);
			alien.y = random.nextInt(SpaceCraft.HEIGHT - alien.height + 1);
			aliens.add(alien);

			lastAlien = System.currentTimeMillis();
		}
	}

}